/*
 * @Author: your name
 * @Date: 2022-03-04 19:52:26
 * @LastEditTime: 2022-03-04 20:42:29
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /ulink-mris-v2/src/views/mris-base/assets/scripts/tools/cookie.js
 */
// 未cookie设置前缀
import storage from '@/assets/scripts/store/user'
import { ACCESS_DEFWS } from '@/store/mutation-types'

// 设置cookie
export function setCookie (keyCode, keyData, exdays) {
  keyCode = (storage.get(ACCESS_DEFWS) || '') + keyCode
  var exdate = new Date() // 获取时间
  exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays) // 保存的天数
  // 字符串拼接cookie
  window.document.cookie = keyCode + '=' + JSON.stringify(keyData) + ';path=/;expires=' + exdate.toGMTString()
}

// 读取cookie
export function getCookie (keyCode) {
  keyCode = (storage.get(ACCESS_DEFWS) || '') + keyCode
  let rCookie = null
  if (document.cookie.length > 0) {
    var arr = document.cookie.split('; ') // 这里显示的格式需要切割一下自己可输出看下
    for (var i = 0; i < arr.length; i++) {
      var arr2 = arr[i].split('=') // 再次切割
      // 判断查找相对应的值
      if (arr2[0] === keyCode) {
        rCookie = JSON.parse(arr2[1])
      }
    }
  }
  return rCookie
}

// 清除cookie
export function clearCookie (keyCode, keyData) {
  setCookie(keyCode, keyData, -1) // 修改2值都为空，天数为负1天就好了
}
